/**
 * 搜索
 * @author Qasim <15750783791@163.com>
 * @version 2016/11/18 1.0 
 */
document.addEventListener('plusready' , ready , false);

var self = '';
function ready(){
	self = plus.webview.currentWebview();
	
	//搜索
	$('.search').on('keydown' , function(e){
		if(e.keyCode == 13){
			var keyWord = $(this).val();
			if(!keyWord.trim()){ 
				toast('关键字不能为空');return;
			}
			search(keyWord);
		}
		
	})
	
	
	//获取热门搜索
	$.ajax({
		type:"post",
		url:apiRoot+"/Home/Article/labelcus/",
		dataType:'json',
		beforeSend:function(){
			plus.nativeUI.showWaiting('加载中...');
		},
		complete:function(){
			plus.nativeUI.closeWaiting();
		},
		success:function(data){
			if(data){
				$.each(data, function( k ,v) {
					$('.list ul').append('<li class="mui-table-view-cell"><i class="mui-icon iconfont gray">&#xe600;</i><span>'+v+'</span></li>');
				});
				
				$('.list ul li').unbind();
				$('.list ul li').on('click' , function(){
					search($(this).find('span').text());
				});
			}
		},
		error:function(e){
			errorPrompt(e);
		}
	});
	
}

/**
 * 搜索
 * @param {String} keyWord [关键字]
 */
function search(keyWord){
	$.ajax({
		type:"post",
		url:apiRoot + "/Home/Article/searchData/",
		data:{
			keywords : keyWord
		},
		beforeSend:function(){
			plus.nativeUI.showWaiting('搜索中...');
		},
		complete:function(){
			plus.nativeUI.closeWaiting();
		},
		success:function(data){
			var result = eval('('+data+')');
			if(result != ''){
				var parameter = {keyword : keyWord , res : result};
				goUrl('search-result.html' , 'search_result' , parameter);
			}else{
				toast('很抱歉 没有搜索到您要的数据');
			}
		},
		error :function(){
			errorPrompt(e);
		}
	});
}

